/**********************************************************

     文件名称: mv_pwd.c

     项目功能: 修改密码 

     作者: 佳奇

     邮箱: 1374804492@qq.com 

     创建时间: Sun 28 Aug 2022 05:10:39 PM CST

**********************************************************/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "sup_mgr.h"
#include "md5.h"


void mv_pwd(admin* demo, int id)
{
	char pwd[33], name[31];
	int cnt = 0;
	char* pass1 = NULL;

	demo = demo->next;

	while(demo != NULL && demo->data->user_id != id)	demo = demo->next;

	printf("\n********修改密码********\n");

	pass1 = getpass("请输入您的原始密码:");
	
	while(++cnt)
	{
		if(strcmp(demo->data->user_pwd, md5_encode(pass1, strlen(pwd))) != 0)
		{
			if(cnt == 3)	
			{
				fprintf(stderr, "\n密码输入错误次数已达上限！请下次再来！\n");
				return;
			}

			fprintf(stderr, "\n原始密码输入错误，请重新输入\n");
			pass1 = getpass("请输入:");
		}else break;
	}
	cnt = 0;
	printf("\n请输入您的新用户名:");
	scanf("%30s", name);
	c_buffer();
	strcpy(demo->data->user_name, name);

	while(++cnt)
	{
		strcpy(pwd, getpass("请输入新密码:"));
		pass1 = getpass("请再次输入新密码:");

		if(strcmp(pwd, pass1) == 0)
		{	
			strcpy(demo->data->user_pwd, md5_encode(pass1, strlen(pass1)));
			demo->data->state = 1;
			printf("\n密码修改成功！\n");
			printf("按任意键返回:");
			getchar();
			c_buffer();
			return;
		}
		else
			printf("\n两次输入密码不一致！请重新输入\n");

		if(cnt == 3)
		{
			fprintf(stderr, "\n错误输入次数过多! 请下次再来\n");
			return;
		}
	}
}

